import Vue from 'vue';
import VueRouter from 'vue-router';
import Film from '@/views/02/Film.vue';
import Cinema from '@/views/02/Cinema.vue';
import Center from '@/views/02/Center.vue';
import NowPlaying from '@/views/02/Film/NowPlaying.vue';
import ComingSoon from '@/views/02/Film/ComingSoon.vue';
import Detail from '@/views/02/Detail.vue';
import City from '@/views/02/City.vue';

Vue.use(VueRouter);

// 判断是否登录
// const auth = {
//     isLogin() {
//         return false;
//     }
// };

const routes = [
    {
        path: '/films',
        // name: 'Film', // 有默认的重定向设置，不用指定name否则页面会有警告
        component: Film,
        children: [
            {
                path: '/films/nowPlaying',
                name: 'nowPlaying',
                component: NowPlaying
            },
            {
                path: 'comingSoon',
                name: 'comingSoon',
                component: ComingSoon
            },
            {
                path: '', // 注意空字符串和*的区别
                redirect: '/films/nowPlaying'
            }
        ]
    },
    {
        path: '/cinema',
        name: 'Cinema',
        alias: '/mycinema', // 路由的别名
        component: Cinema
    },
    {
        path: '/city',
        name: 'City',
        component: City
    },
    {
        path: '/center',
        name: 'Center',
        component: Center
    },
    {
        path: '/detail/:filmId', // 动态路由，:表示参数名
        name: 'Detail',
        component: Detail,
        props: true // 以props形式传参
    },
    {
        path: '*',
        redirect: '/films/nowPlaying'
    }
];

const router = new VueRouter({
    mode: 'history', // url中默认使用hash模式，即url中会有#，history模式不带#但会向后端发请求，但风险是如果后端没有这个路由，页面会返回404，需要web服务配置无路由匹配时渲染index.html页面
    base: process.env.BASE_URL,
    routes
});

// 全局路由守卫
// router.beforeEach((to, from, next) => {
//     if (to.path === '/center') {
//         if (auth.isLogin()) {
//             next();
//         } else {
//             console.log('未登录，全局路由守卫触发');
//             next('/login');
//         }
//     } else {
//         next();
//     }
// });

export default router;
